package com.ma.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ma.model.process.ProcessTemplate;
import com.ma.model.process.ProcessType;
import com.ma.process.mapper.OaProcessTemplateMapper;
import com.ma.process.service.OaProcessTemplateService;
import com.ma.process.service.OaProcessTypeService;
import com.ma.process.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class OaProcessTemplateServiceImpl extends ServiceImpl<OaProcessTemplateMapper, ProcessTemplate> implements OaProcessTemplateService {
    @Autowired
    private OaProcessTypeService processTypeService;
    @Autowired
    private ProcessService processService;

    @Override
    public Page<ProcessTemplate> index(Long page, Long limit) {
        Page<ProcessTemplate> templatePage = new Page<>(page,limit);
        List<ProcessTemplate> templates = templatePage.getRecords();
        //遍历模板列表，得到每个模板的审批类型的id，根据id查询审批类型
        for(ProcessTemplate template : templates){
            Long processTypeId = template.getProcessTypeId();
            ProcessType processType = processTypeService.getOne(new LambdaQueryWrapper<ProcessType>().eq(ProcessType::getId, processTypeId));
            if (processType == null){
                continue;
            }
            //完成封装
            template.setProcessTypeName(processType.getName());
        }
        return templatePage;
    }


    @Override
    @Transactional
    public void publish(Long id) {
        ProcessTemplate template = this.getById(id);
        template.setStatus(1);
        this.updateById(template);
        //部署流程定义
        if (template.getProcessDefinitionPath() != null){
            processService.deployByZip(template.getProcessDefinitionPath());
        }
    }
}
